#region imported Namespaces
using System;
using System.Data;
using System.Data.SqlClient;

using BO = PHSRAG.Insight21.BusinessObjects;
using Utility = PHSRAG.Utility;
using ICachePolicy = PHSRAG.Insight21.Policy.ICachePolicy;
using GenerateKey = PHSRAG.Insight21.BusinessObjects.ModuleKey;
#endregion

namespace PHSRAG.Insight21.Agreements
{
	/// <summary>
	/// Summary description for ICRate.
	/// </summary>
	public class ICRate : BO.BusinessObject
	{
		#region Constants
		private static string ICRateKey = GenerateKey.MakeKey("ICRate_9B1D6AAD-E22F-4995-9618-A90F4DB9D208",GenerateKey.Agreement);
		#endregion

		#region Constructor
		public ICRate(ICachePolicy cachePolicy) : base(cachePolicy)
		{
			DataContainer = new DataSet();
		}
		#endregion

		#region private members
		private const string ICDataTableName = "ICRate";
		#endregion

		#region Properties
		public decimal Rate
		{
			get
			{
				if(DataContainer.Tables[ICDataTableName].Rows.Count == 1)
				{	
					object o = DataContainer.Tables[ICDataTableName].Rows[0]["Rate"];
					return (o == DBNull.Value) ? -1 : (decimal)o;
				}
				else return decimal.MinValue;
			}
		}

		public int NumberOfMonths
		{
			get
			{ 
				object o = DataContainer.Tables[ICDataTableName].Rows[0]["NumberOfMonths"];
				return (o == DBNull.Value) ? -1 : (int)o;
			}
		}

		public int SequenceNo
		{
			get
			{
				object o = DataContainer.Tables[ICDataTableName].Rows[0]["SequenceNo"];
				return (o == DBNull.Value) ? -1 : (int)o;
			}
		}

		public string EffectiveDate
		{
			get
			{
				object o = DataContainer.Tables[ICDataTableName].Rows[0]["EffectiveDate"];
				return (o == DBNull.Value) ? "" : ((DateTime)o).ToString("MM/dd/yy");
			}
		}
		#endregion

		#region Base Methods
		public override void Cache()
		{
			CachePolicy.Cache(ICRateKey, this);
		}

		public override void UnCache()
		{
			CachePolicy.UnCache(ICRateKey);
		}

		public override void Load(string connectionString, params object[] args )
		{
			if ( args.Length != 2 )
				throw new Exception("IC Rate Load requires Budget Container Id and SequenceNo.");

			// get the variables.
			int agrBudgetContainerId = (int)args[0];
			int sequenceNo = (int)args[1];

			try
			{
				DataContainer.Reset();

				Utility.DataAccess.LoadDataSet(DataContainer, 
					ICDataTableName, 
					connectionString, 
					"GetICRate", 
					Utility.DataAccess.BuildSqlParameterArray(
							"@agrBudgetContainerId" ,SqlDbType.Int, agrBudgetContainerId,
							"@sequenceNo" ,SqlDbType.Int,sequenceNo ));
			}
			catch( Exception e)
			{
				Utility.ExceptionReport.WriteToEventLog ("ICRate Load", e, true, Utility.ExceptionReport.ReportType.Error);
			}
		}
		#endregion
	}
}
